package com.acoreful.acf2java.commons.beans.response;

import java.util.List;

import org.springframework.data.domain.Page;

import com.google.common.collect.Lists;

import lombok.Data;
/**
 *
 */
@Data
public class DataTablesResponse {
	private Integer draw;
	private Long recordsTotal;
	private Long recordsFiltered;
	private String error;
	private List<?> data = Lists.newArrayList();
	
	private DataTablesResponse(Integer draw, Page<?> page,String error) {
		this.draw = draw;
		this.recordsTotal = this.recordsFiltered = page.getTotalElements();
		this.error=error;
		this.data = page.getContent();
	}
	
	/**
	 * 格式化分页数据, 使符合jQueryDataTables 1.10的要求
	 * 
	 * @param draw 绘制表格的次数
	 * @param page SpringDataJpa的分页数据对象
	 * @return
	 */
	public static DataTablesResponse format(Integer draw, Page<?> page) {
		return new DataTablesResponse(draw, page,null);
	}
	/**
	 * 格式化分页数据, 使符合jQueryDataTables 1.10的要求
	 * 
	 * @param draw 绘制表格的次数
	 * @param error 错误信息
	 * @param page SpringDataJpa的分页数据对象
	 * @return
	 */
	public static DataTablesResponse format(Integer draw, Page<?> page,String error) {
		return new DataTablesResponse(draw, page,error);
	}
	
}